<?php
/**
 * @desc 订单数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Order extends Base
{
	//数据表
	protected $table = 'pert_order';
	protected $dtable = 'pert_order_detail';
	protected $ptable = 'pert_passenger_info';
	protected $gtable = 'pert_goods';
	//写入订单数据
	public function insertOrder(array $info, array $order, array $orderDetail){
		$sqls = array();
		$binds = array();
		if(empty($info) || empty($order) || empty($orderDetail)){
			return false;
		}
		//主订单数据写入sql
		$table = $this->table;
		$sql = "insert into `{$table}` set ";
		$bind = array();
		foreach($order as $key => $value){
			$sql .= "`{$key}` = :{$key},";
			$bind[":{$key}"] = $value;
		}
		$sqls[] = $sql."`raw_add_time` = NOW()";
		$binds[] = $bind;
		//子订单数据写入sql
		$table = $this->dtable;
		foreach($orderDetail as $v){
			$sql = "insert into `{$table}` set ";
			$bind = array();
			foreach($v as $key => $value){
				$sql .= "`{$key}` = :{$key},";
				$bind[":{$key}"] = $value;
			}
			$sqls[] = $sql."`raw_add_time` = NOW()";
			$binds[] = $bind;
		}
		//旅客信息写入sql
		$table = $this->ptable;
		foreach($info as $v){
			$sql = "insert into `{$table}` set ";
			$bind = array();
			foreach($v as $key => $value){
				$sql .= "`{$key}` = :{$key},";
				$bind[":{$key}"] = $value;
			}
			$sqls[] = $sql."`raw_add_time` = NOW()";
			$binds[] = $bind;
		}
		//事务写入数据
		return $this->transaction($sqls, $binds);
	}
	//获取主订单
	public function getOrderMain($orderSn, $userId){
		$table = $this->table;
		$filed = "`order_sn`,`user_id`,`total_amount`,`order_status`,`pay_status`,`order_date`,`order_expire`,`travel_date`";
		$sql = "select {$filed} from `{$table}` where `order_sn` = :order_sn and `user_id` = :user_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':order_sn'=>$orderSn,':user_id'=>$userId), Base::QUERY_RAW);
	}
	//获取订单信息
	public function getOrderInfo($orderSn, $userId){
		$table = $this->table;
		$dtable = $this->dtable;
		$gtable = $this->gtable;
		$filed = "a.`order_sn`,a.`user_id`,a.`total_amount`,a.`order_status`,a.`pay_status`,a.`order_date`,a.`order_expire`,a.`travel_date`,c.`goods_name`,b.`sell_num`,b.`goods_sn`,c.`image`,a.`is_travel`";
		$sql = "select {$filed} from `{$table}` as a left join `{$dtable}` as b on a.`order_sn` = b.`order_sn` left join `{$gtable}` as c on b.`goods_sn` = c.`goods_sn` where a.`delete_flag` = 0 and a.`order_sn` = :order_sn and a.`user_id` = :user_id group by  a.`order_sn`";
		return $this->select_table($sql, array(':order_sn'=>$orderSn,':user_id'=>$userId), Base::QUERY_RAW);
	}
	//获取订单详情
	public function getOrderDetail($orderSn){
		$table = $this->dtable;
		$gtable = $this->gtable;
		$filed = "a.`order_sn`,a.`order_detail_sn`,a.`goods_sn`,a.`sell_num`,a.`price`,a.`total_price`,a.`detail_status`,a.`pay_status`,a.`raw_add_time`,b.`goods_name`";
		$sql = "select {$filed} from `{$table}` as a,`{$gtable}` as b where `order_sn` = :order_sn and a.`goods_sn` = b.`goods_sn` and a.`delete_flag` = 0";
		return $this->select_table($sql, array(':order_sn'=>$orderSn));
	}
	//获取订单列表
	public function getOrderList($userId, $currentPage = 1, $pagePerNum = 5){
		$table = $this->table;
		$dtable = $this->dtable;
		$gtable = $this->gtable;
		//数据获取
		$filed = "a.`order_sn`,a.`user_id`,a.`total_amount`,a.`order_status`,a.`pay_status`,a.`order_date`,a.`order_expire`,a.`travel_date`,c.`goods_name`,b.`sell_num`,b.`goods_sn`,c.`image`,a.`is_travel`";
		$sql = "select {$filed} from `{$table}` as a left join `{$dtable}` as b on a.`order_sn` = b.`order_sn` left join `{$gtable}` as c on b.`goods_sn` = c.`goods_sn` where a.`user_id` = :user_id and a.`delete_flag` = 0 group by  a.`order_sn`";
		$limit = " limit ".(($currentPage - 1)*$pagePerNum).", ".$pagePerNum;
		$return = $this->select_table($sql.$limit, array(':user_id'=>$userId));
		//总数获取
		$sql = "select count(a.`order_sn`) as num from `{$table}` as a left join `{$dtable}` as b on a.`order_sn` = b.`order_sn` left join `{$gtable}` as c on b.`goods_sn` = c.`goods_sn` where a.`user_id` = :user_id and a.`delete_flag` = 0";
		$return[] = $this->select_table($sql, array(':user_id'=>$userId), Base::QUERY_RAW);
		return $return;
	}
	//修改订单状态
	public function updateOrderStatus($status, $orderSn){
		$table = $this->table;
		$dtable = $this->dtable;
		$sqls[] = "update `{$table}` set `pay_status` = :pay_status where `order_sn` = :order_sn and `delete_flag` = 0";
		$binds[] = array(':pay_status' => $status,':order_sn' => $orderSn);
		$sqls[] = "update `{$dtable}` set `pay_status` = :pay_status where `order_sn` = :order_sn and `delete_flag` = 0";
		$binds[] = array(':pay_status' => $status,':order_sn' => $orderSn);
		return $this->transaction($sqls, $binds);
	}
}
